package com.yunhe.video.utils;

import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhengbo
 * @date 2021/3/23 14:55
 * 对接海康视频平台
 */
public class HkvsSdk {

    /**
     * 控制视频
     * @param cameraId 视频id
     * @param command   指令
     * @param action    动作
     * @param speed     速度
     * @return
     */
    public static String controlVideo(String cameraId, String command, String action, String speed) throws JSONException {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = "222.212.91.5:81"; // artemis网关服务器ip端口
        ArtemisConfig.appKey = "26850770";  // 秘钥appkey
        ArtemisConfig.appSecret = "uB8rGoZFjj59OF21HkJw"; // 秘钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        String previewURLsApi = ARTEMIS_PATH + "/api/video/v1/ptzs/controlling";
        Map<String, String> path = new HashMap<>(2);
        path.put("http://", previewURLsApi);

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraId);
        if (StringUtils.isNotEmpty(action)) {
            jsonBody.put("action", Integer.parseInt(action)); //0-开始 1-停止
        } else {
            jsonBody.put("action", 0);
        }



        jsonBody.put("command", command);
        if (StringUtils.isNotEmpty(speed)) {
            jsonBody.put("speed", Integer.parseInt(speed));
        } else {
            jsonBody.put("speed", 50);
        }
//    jsonBody["presetIndex"] = ""
        String body = jsonBody.toString();
        return ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
    }

    public static String getCameraList() throws JSONException {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */

        ArtemisConfig.appKey = "26850770";  // 秘钥appkey
        ArtemisConfig.appSecret = "uB8rGoZFjj59OF21HkJw"; // 秘钥appSecret
        ArtemisConfig.host = "222.212.91.5:81"; // artemis网关服务器ip端口
        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        String previewURLsApi = ARTEMIS_PATH + "/api/resource/v1/camera/advance/cameraList";
        Map<String, String> path = new HashMap<>(2);
        path.put("http://", previewURLsApi); //根据现场环境部署确认是http还是https

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 500);
        String body = jsonBody.toString();
        System.out.println(body);
        return ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
    }

    public static void main(String[] args) {
        try {
            System.out.println(getCameraList());
//        System.out.println(controlVideo("d956fc4b9255413c98f260240921c8e2","LEFT","0","50"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
